﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Data;
using System.Windows.Forms;
using System.IO;


public class IniFile
{
    // Fields
    public static string FileName = "";

    // Methods
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
    public static bool ReadBool(string section, string key, bool def)
    {
        string s = ReadString(section, key, "");
        return ((s == "1") || (s.ToLower() == "true"));
    }

    public static double ReadFloat(string section, string key, double def)
    {
        string s = ReadString(section, key, "");
        if (s == "")
        {
            return def;
        }
        try
        {
            return Convert.ToDouble(s);
        }
        catch
        {
            return def;
        }
    }

    public static int ReadInt(string section, string key, int def)
    {
        string s = ReadString(section, key, "");
        if (s == "")
        {
            return def;
        }
        try
        {
            return Convert.ToInt32(s);
        }
        catch
        {
            return def;
        }
    }

    public static string ReadString(string section, string key, string def)
    {
        StringBuilder sb = new StringBuilder(200);
        GetPrivateProfileString(section, key, def, sb, 200, FileName);
        return sb.ToString();
    }

    [DllImport("kernel32")]
    public static extern long WinExec(string command, uint i);
    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    public static void WriteString(string section, string key, string val)
    {
        WritePrivateProfileString(section, key, val, FileName);
    }


    private static void SaveControl(List<string> ls, Control ct, DataTable dt)
    {
        string s = ct.ToString().Split(',')[0];
        if (ls.IndexOf(s) == -1 && !dt.Columns.Contains(ct.Name))
        {
            ls.Add(s);
            Console.WriteLine(s);
        }
        switch (s)
        {
            case "System.Windows.Forms.TextBox":
                if (!dt.Columns.Contains(ct.Name)) dt.Columns.Add(ct.Name);
                if (dt.Rows.Count > 0) dt.Rows[0][ct.Name] = (ct as System.Windows.Forms.TextBox).Text;
                break;
            case "System.Windows.Forms.ComboBox":
                if (!dt.Columns.Contains(ct.Name)) dt.Columns.Add(ct.Name);
                if (dt.Rows.Count > 0) dt.Rows[0][ct.Name] = (ct as System.Windows.Forms.ComboBox).Text;
                break;
            case "System.Windows.Forms.CheckBox":
                if (!dt.Columns.Contains(ct.Name)) dt.Columns.Add(ct.Name, typeof(bool));
                if (dt.Rows.Count > 0) dt.Rows[0][ct.Name] = (ct as System.Windows.Forms.CheckBox).Checked;
                break;
            case "System.Windows.Forms.NumericUpDown":
                if (!dt.Columns.Contains(ct.Name)) dt.Columns.Add(ct.Name, typeof(decimal));
                if (dt.Rows.Count > 0) dt.Rows[0][ct.Name] = (ct as System.Windows.Forms.NumericUpDown).Value;
                break;
        }
        foreach (Control ct2 in ct.Controls)
        {
            SaveControl(ls, ct2, dt);
        }
    }
    private static void LoadControl(List<string> ls, Control ct, DataTable dt)
    {
        string s = ct.ToString().Split(',')[0];
        if (ls.IndexOf(s) == -1 && !dt.Columns.Contains(ct.Name))
        {
            ls.Add(s);
            Console.WriteLine(s);
        }
        try
        {
            switch (s)
            {
                case "System.Windows.Forms.TextBox":
                    if (dt.Columns.Contains(ct.Name))
                        (ct as System.Windows.Forms.TextBox).Text = dt.Rows[0][ct.Name].ToString();
                    break;
                case "System.Windows.Forms.ComboBox":
                    if (dt.Columns.Contains(ct.Name))
                        (ct as System.Windows.Forms.ComboBox).Text = dt.Rows[0][ct.Name].ToString();
                    break;
                case "System.Windows.Forms.CheckBox":
                    if (dt.Columns.Contains(ct.Name) && dt.Columns[ct.Name].DataType == typeof(Boolean))
                        if (dt.Rows[0][ct.Name] != DBNull.Value) (ct as System.Windows.Forms.CheckBox).Checked = Convert.ToBoolean(dt.Rows[0][ct.Name]);
                    break;
                case "System.Windows.Forms.NumericUpDown":
                    if (dt.Columns.Contains(ct.Name) && dt.Columns[ct.Name].DataType == typeof(decimal))
                        if (dt.Rows[0][ct.Name] != DBNull.Value) (ct as System.Windows.Forms.NumericUpDown).Value = Convert.ToDecimal(dt.Rows[0][ct.Name]);
                    break;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ct.Name + " " + ex.Message);
        }
        foreach (Control ct2 in ct.Controls)
        {
            LoadControl(ls, ct2, dt);
        }
    }
    public static void SaveFormSet(Form myfrm)
    {
        string spath = AppDomain.CurrentDomain.BaseDirectory + "myset.xml";
        SaveFormSet(myfrm, spath);
    }
    public static void SaveFormSet(Form myfrm, string SavePath)
    {
        List<string> ls = new List<string>();
        DataTable dt = new DataTable("myset");
        foreach (Control ct in myfrm.Controls)
        {
            SaveControl(ls, ct, dt);
        }

        dt.Rows.Clear();
        if (File.Exists(SavePath))
        {
            dt.ReadXml(SavePath);
        }
        if (dt.Rows.Count > 0)
        {
            ls = new List<string>();
            foreach (Control ct in myfrm.Controls)
            {
                LoadControl(ls, ct, dt);
            }
        }
        myfrm.FormClosing += delegate (object sender, FormClosingEventArgs e)
        {
            if (dt.Rows.Count < 1)
            {
                dt.Rows.Add(dt.NewRow());
            }

            ls = new List<string>();
            foreach (Control ct in myfrm.Controls)
            {
                SaveControl(ls, ct, dt);
            }
            dt.AcceptChanges();
            dt.WriteXml(SavePath);
        };
    }

    // Properties
    public static string RelationFileName
    {
        get
        {
            return FileName;
        }
        set
        {
            FileName = AppDomain.CurrentDomain.BaseDirectory + value;
        }
    }
}


